$(document).ready(function(){
    // create dialog for message form
    var sendMessageDialog = new DialogBox($('#sendMessage'))
    $('input[name=selectMessage]').change(function(){
        if($(this).attr('checked'))
        {
            MAX.ajax('get', $(this).val(), '', function(html){
                $('#messages').html(html)
            })
        }

    })
    $('input[name=selectMessage]').first().attr('checked',true)
    $('input[name=selectMessage]').trigger('change')
    // for first time get data of first value
    //    MAX.ajax('get', $('input[name=selectMessage]').first().val(), '', function(html){
    //        $('#messages').html(html)
    //    })
    $('input[name=selectMessage]').first().attr('checked',true)
    
    $('#sendNewMessage').click(function(){
        MAX.ajax('GET', $(this).attr('href'), '', function(res){
            sendMessageDialog.html(res.html)
            sendMessageDialog.open()
        })
        return false
    })
    $('#sendMessage').on('submit','#sendMessageForm',function(e){
        MAX.ajax('POST',$(this).attr('action'),$(this).serialize(),function(res){
            if(res.valid)
            {
                sendMessageDialog.close()
                $('input[name=selectMessage]').first().attr('checked',true)
                $('input[name=selectMessage]').trigger('change')
            }
            else
            {
                sendMessageDialog.html(res.html)
            }
        })
        return false
    })
})
